////////////////////////////////////////////////////////////////////////////////

#ifndef _CLIENTBATTLEFIELD_H_
#include "clientbattlefield.h"
#endif

////////////////////////////////////////////////////////////////////////////////

#ifndef _SERVERSTATE_H_
#include "serverstate.h"
#endif

#include <cmath>
#include <algorithm>

////////////////////////////////////////////////////////////////////////////////


ClientBattlefield::ClientBattlefield()
{
}


////////////////////////////////////////////////////////////////////////////////


ClientBattlefield::~ClientBattlefield()
{
}


////////////////////////////////////////////////////////////////////////////////


void ClientBattlefield::Integrate( const ServerState& _next,
                                   double _alpha )
{
    IntegrateObject( m_Players, _next.GetPlayers(), _alpha );
    IntegrateObject( m_Bullets, _next.GetBullets(), _alpha );

    // Check for new players
    PlayersList players = _next.GetPlayers();
    for ( PlayersList::iterator it = players.begin(); it != players.end(); ++it)
    {
        if ( std::find(m_Players.begin(), m_Players.end(), *it) == m_Players.end() )
            m_Players.push_back(*it);
    }

    // Check for new bullets
    BulletsList bullets = _next.GetBullets();
    for ( BulletsList::iterator it = bullets.begin(); it != bullets.end(); ++it)
    {
        if ( std::find(m_Bullets.begin(), m_Bullets.end(), *it) == m_Bullets.end() )
            m_Bullets.push_back(*it);
    }

    // Check for new walls
    WallsList walls = _next.GetWalls();
    for ( WallsList::iterator it = walls.begin(); it != walls.end(); ++it)
    {
        if ( std::find(m_Walls.begin(), m_Walls.end(), *it) == m_Walls.end() )
            m_Walls.push_back(*it);
    }
}


////////////////////////////////////////////////////////////////////////////////


//template<typename TObj>
//void ClientBattlefield::IntegrateObject( TObj& _curObjects, const TObj& _nextObjects, double _alpha )
//{
//    for ( TObj::iterator itCur = _curObjects.begin(); itCur != _curObjects.end(); ++itCur )
//    {
//        TObj::const_iterator itNext = std::find(_nextObjects.begin(), _nextObjects.end(), *itCur);
//        if ( itNext != _nextObjects.end() )
//        {
//            PE::Point nextPosition;
//            nextPosition.x = itCur->GetPosition().x * _alpha + itNext->GetPosition().x * (1.0 - _alpha);
//            nextPosition.y = itCur->GetPosition().y * _alpha + itNext->GetPosition().y * (1.0 - _alpha);
//            itCur->SetPosition(nextPosition);
//        }
//    }
//}


////////////////////////////////////////////////////////////////////////////////
